
import requests
from bs4 import BeautifulSoup

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'}


for x in range(10):
    url = 'https://movie.douban.com/top250?start={}&filter='.format(x*25)
    print(url)
    res = requests.get(url, headers=headers)
    soup = BeautifulSoup(res.text, 'html.parser')
    li_list = soup.find('ol', class_='grid_view').find_all('li')
    # 分析数据
    for tag in li_list:
        num = tag.find('em', class_='').text   #排序
        title = tag.find('span', class_='title').text
        star = tag.find('span', class_='rating_num').text
        hd_url = tag.find('a')['href']
        star_all = tag.find('div', class_='star').findAll('span')[-1].text
        # 判断有没有推荐语
        if tag.find('span', class_='inq') != None:
            quote = tag.find('span', class_='inq').text
            print('序号：' + num + '\n' +
                  '电影名：' + title + '\n' +
                  '评分：' + star + '\n' +
                  '评价人数：' + star_all + '\n' +
                  '推荐语：' + quote + '\n' +
                  '链接：' + hd_url + '\n\n')
        else:
            print('序号：' + num + '\n' +
                  '电影名：' + title + '\n' +
                  '评分：' + star + '\n' +
                  '链接：' + hd_url + '\n\n')